#pragma once
#include<iostream>
#include<vector>
using namespace std;
namespace My_pq
{
    template <class T, class Container = vector<T>, class Compare = less<T> >
    class priority_queue
    {

    public:

        priority_queue()
            :c()
            ,comp()
        { }
        void adjust_down(int parent)
        {
            int child = parent * 2 + 1;
            while (child < c.size())
            {
                if (child + 1 < c.size() && c[child] > c[child + 1])
                {
                    child++;
                }
                if (c[parent] > c[child])
                {
                    swap(c[parent], c[child]);
                    parent = child;
                    child = parent * 2 + 1;
                }
                else
                    break;
            }
        }
        void adjust_up(int child)
        {
            int parent = (child - 1) / 2;
            while (child > 0)
            {
                if (c[parent] > c[child])
                {
                    swap(c[parent], c[child]);
                    child = parent;
                    parent = child / 2 - 1;
                }
                else
                    break;
            }
        }
        template <class InputIterator>
        priority_queue(InputIterator first, InputIterator last)
        {
            while (first != last)
            {
                c.push_back(*first);
                ++first;
            }
            for (int i = (c.size() - 1 - 1) / 2; i >= 0; i--)
            {
                adjust_down(i);
            }
        }

        bool empty() const
        {
            return c.empty();
        }

        size_t size() const
        {
            return c.size();
        }

        const T& top() const
        {
            return c.front();
        }

        void push(const T& x)
        {
            c.push_back(x);
            adjust_up(c.size()-1);
        }

        void pop()
        {
            swap(c[0], c[c.size() - 1]);
            c.pop_back();

            adjust_down(0);
        }

    private:

        Container c;

        Compare comp;

    };
    void test_pq1()
    {
        int arr[] = { 9,5,6,3,2,8,7,1,4 };
        priority_queue<int> pq1(&arr[0], &arr[8]);
        while (!pq1.empty())
        {
            cout << pq1.top() << " ";
            pq1.pop();
        }

    }
};